package cn.shentianlan.service.impl;


import cn.shentianlan.criteria.TeacherCriteria;
import cn.shentianlan.domain.dto.TeacherDTO;
import cn.shentianlan.domain.dto.UserDTO;
import cn.shentianlan.domain.entity.Teacher;
import cn.shentianlan.domain.entity.User;
import cn.shentianlan.domain.enums.GenderCode;
import cn.shentianlan.domain.enums.RoleNameCode;
import cn.shentianlan.mapper.TeacherMapper;
import cn.shentianlan.mapper.UserMapper;
import cn.shentianlan.service.TeacherService;
import cn.shentianlan.utils.BaseUtils;
import cn.shentianlan.utils.DataUtils;
import cn.shentianlan.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 陈俊源
 * @Description TODO
 * @Date 2021/3/27
 * @Version 1.0
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private UserMapper userMapper;
    private TeacherDTO teacherDTO;

    @Transactional
    @Override
    public Result saveOrUpdate(TeacherDTO teacherDTO) {
        Result result = checkTeacherInfo(teacherDTO);
        if (!Result.isSuccess(result)) {
            return result;
        }
        Teacher teacher = BaseUtils.copyProperties(teacherDTO, Teacher.class);
        User user = BaseUtils.copyProperties(teacherDTO, User.class);
        if (teacherDTO.getId() == null) {
            Long id = DataUtils.assignId();
            user.setId(id);
            teacher.setId(id);
            userMapper.saveUser(user);
            teacherMapper.saveTeacher(teacher);
        } else {
            userMapper.updateUser(user);
            teacherMapper.updateTeacher(teacher);
        }
        return Result.success(teacher.getId());
    }

    @Override
    public Result getTeacherById(Long id) {
        if (id == null) {
            return Result.fail("id为空，无法获取该id对应的教师信息");
        }
        Teacher teacher = teacherMapper.getTeacherById(id);
        if (teacher == null) {
            return Result.fail("无法找到该id对应的教师信息");
        }
        TeacherDTO teacherDTO = bulidTeacherDTO(teacher);
        return Result.success(teacherDTO);
    }

    @Override
    public Result findTeacherByCriteria(TeacherCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize(), false);
        List<Teacher> teacherList = teacherMapper.findTeacherByCriteria(criteria);
        Integer count = teacherMapper.getCount(criteria);
        List<TeacherDTO> teacherDTOList = new ArrayList<TeacherDTO>();
        for (Teacher teacher : teacherList) {
            TeacherDTO teacherDTO = bulidTeacherDTO(teacher);
            teacherDTOList.add(teacherDTO);
        }
        PageInfo<TeacherDTO> pageInfo = new PageInfo<TeacherDTO>(teacherDTOList);
        pageInfo.setTotal(count.longValue());
        return Result.success(pageInfo);
    }

    @Transactional
    @Override
    public Result deleteTeacherById(Long id) {
        if (id == null) {
            return Result.fail("id为空，无法执行删除操作");
        }
        userMapper.deleteUserById(id);
        teacherMapper.deleteTeacherById(id);
        return Result.success(id);
    }

    public TeacherDTO bulidTeacherDTO(Teacher teacher) {
        TeacherDTO teacherDTO = BaseUtils.copyProperties(teacher, TeacherDTO.class);
        teacherDTO.setGenderDec(GenderCode.getValue(teacherDTO.getGender()));
        teacherDTO.setRoleNameDec(RoleNameCode.getValue(teacherDTO.getRoleName()));
        return teacherDTO;
    }

    /**
     * 校验DTO信息
     *
     * @param teacherDTO
     * @return
     */
    //todo 检验用户信息步骤不完善，缺失更新校验
    private Result checkTeacherInfo(TeacherDTO teacherDTO) {
        if (teacherDTO == null) {
            return Result.fail("数据传输对象不能为空");
        }
//        if (teacherDTO.getUserName()==null){
//            return Result.fail("用户账号不能为空！");
//        }
//        if (teacherDTO.getFullName()==null){
//            return Result.fail("用户姓名不能为空！");
//        }
//        if (teacherDTO.getPassword()==null){
//            return Result.fail("用户密码不能为空！");
//        }
//        if (teacherDTO.getBirthDay()==null){
//            return Result.fail("用户出生日期不能为空！");
//        }
//        if (teacherDTO.getGender()==null){
//            return Result.fail("用户性别不能为空！");
//        }
//        if (teacherDTO.getContactWay()==null){
//            return Result.fail("用户联系方式不能为空！");
//        }
//        if (teacherDTO.getSalary()==null){
//            return Result.fail("工资不能为空！");
//        }
//        if (teacherDTO.getLeaveDays()==null){
//            teacherDTO.setLeaveDays(0.0);
//        }
//        if (teacherDTO.getId()!=null){
//            Teacher teacher = teacherMapper.getTeacherById(teacherDTO.getId());
//            if (teacher!=null){
//                return Result.fail("查询不到该教师信息，数据更新失败");
//            }
//        }
        return Result.success();
    }
}
